<template>
  <el-dialog
    :title="!dataForm.trId ? '录入棉花土壤信息' : '修改棉花土壤信息'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="编号" prop="mhBh">
            <el-select v-model="dataForm.mhBh" filterable  placeholder="请选择" disabled>
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="8">
          <el-form-item label="有机质含量" prop="trYjzhl" style = "white-space:nowrap;">
            <el-input v-model="dataForm.trYjzhl" placeholder="有机质含量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="全氮含量" prop="trQdhl">
            <el-input v-model="dataForm.trQdhl" placeholder="全氮含量"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="碱解氮含量" prop="trJjdhl" style = "white-space:nowrap;">
          <el-input v-model="dataForm.trJjdhl" placeholder="碱解氮含量"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="有效磷含量" prop="trYxlhl" style = "white-space:nowrap;">
          <el-input v-model="dataForm.trYxlhl" placeholder="有效磷含量"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="速效磷含量" prop="trSxlhl" style = "white-space:nowrap;">
          <el-input v-model="dataForm.trSxlhl" placeholder="速效磷含量"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="pH值" prop="trPh">
            <el-input v-model="dataForm.trPh" placeholder="pH 值"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="鲜重" prop="trXz">
            <el-input v-model="dataForm.trXz" placeholder="鲜重"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="干重" prop="trGz">
            <el-input v-model="dataForm.trGz" placeholder="干重"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    <el-form-item label="土壤含水量" prop="trTrhsl" style = "white-space:nowrap;">
      <el-input v-model="dataForm.trTrhsl" placeholder="土壤含水量"></el-input>
    </el-form-item >
      <el-row  v-if="isFieldsDisabled">
        <el-form-item label="施肥建议" prop="trSfjy" style = "white-space:nowrap;" >
          <el-input v-model="dataForm.trSfjy" placeholder="施肥建议"></el-input>
        </el-form-item>
      </el-row>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        options: [],
        visible: false,
        dataForm: {
          trId: 0,
          mhBh: null,
          trYjzhl: '',
          trQdhl: '',
          trJjdhl: '',
          trYxlhl: '',
          trSxlhl: '',
          trPh: '',
          trXz: '',
          trGz: '',
          trTrhsl: '',
          trSfjy: ''
        },
        dataRule: {
          trYjzhl: [
            { required: true, message: '有机质含量不能为空', trigger: 'blur' }
          ],
          trQdhl: [
            { required: true, message: '全氮含量不能为空', trigger: 'blur' }
          ],
          trJjdhl: [
            { required: true, message: '碱解氮含量不能为空', trigger: 'blur' }
          ],
          trYxlhl: [
            { required: true, message: '有效磷含量不能为空', trigger: 'blur' }
          ],
          trSxlhl: [
            { required: true, message: '速效磷含量不能为空', trigger: 'blur' }
          ],
          trPh: [
            { required: true, message: 'pH 值不能为空', trigger: 'blur' }
          ],
          trXz: [
            { required: true, message: '鲜重不能为空', trigger: 'blur' }
          ],
          trGz: [
            { required: true, message: '干重不能为空', trigger: 'blur' }
          ],
          trTrhsl: [
            { required: true, message: '土壤含水量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      'dataForm.trPh': function (newValue) {
        this.isFieldsDisabled = this.isPhOutOfRange(newValue)
      }
    },
    methods: {
      isPhOutOfRange (ph) {
        return ph < 3 || ph > 11
      },
      handlePhInput () {
        this.isFieldsDisabled = this.isPhOutOfRange(this.dataForm.trPh)
      },
      init (id) {
        this.dataForm.trId = id || 0
        this.visible = true
        this.getBhList()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.trId) {
            this.$http({
              url: this.$http.adornUrl(`/mh/tr/info/${this.dataForm.trId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mhBh = data.tr.mhBh
                this.dataForm.trYjzhl = data.tr.trYjzhl
                this.dataForm.trQdhl = data.tr.trQdhl
                this.dataForm.trJjdhl = data.tr.trJjdhl
                this.dataForm.trYxlhl = data.tr.trYxlhl
                this.dataForm.trSxlhl = data.tr.trSxlhl
                this.dataForm.trPh = data.tr.trPh
                this.dataForm.trXz = data.tr.trXz
                this.dataForm.trGz = data.tr.trGz
                this.dataForm.trTrhsl = data.tr.trTrhsl
                this.dataForm.trSfjy = data.tr.trSfjy
              }
            })
          }
        })
      },
      getBhList () {
        this.$nextTick(() => {
          this.$http({
            url: this.$http.adornUrl('/mh/tr/listbh'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log('list' + JSON.stringify(data.list))
              this.options = data.list.map(item => ({
                value: item,
                label: item
              }))
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mh/tr/${!this.dataForm.trId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'trId': this.dataForm.trId || undefined,
                'mhBh': this.dataForm.mhBh,
                'trYjzhl': this.dataForm.trYjzhl,
                'trQdhl': this.dataForm.trQdhl,
                'trJjdhl': this.dataForm.trJjdhl,
                'trYxlhl': this.dataForm.trYxlhl,
                'trSxlhl': this.dataForm.trSxlhl,
                'trPh': this.dataForm.trPh,
                'trXz': this.dataForm.trXz,
                'trGz': this.dataForm.trGz,
                'trTrhsl': this.dataForm.trTrhsl,
                'trSfjy': this.dataForm.trSfjy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
